<?php
/**
 * Author Yulin
 * Email  dwzhanglong@126.com
 * Date   2017-7-27
 * Func   遗漏计算
 */
defined('IN_YuLin') || exit('NO PERMIT!');

$table = Table('lottery_period_lost');
$itable = Table('lottery_period');

//	号码长度
$lens = [1=>9,];

//	北京PK10单独计算
$data = $db->getall('SELECT * FROM '.$itable.' WHERE id NOT IN (SELECT iid FROM '.$table.') AND rednum <> \'\' ORDER BY stoptime LIMIT 500');
foreach($data as $val)
{
	$rednum = explode(',',$val['rednum']);
	$len = count($rednum);	//	位数
	
	$shuju = [];
	for($j = 0; $j < $len; $j++)
	{
		$weizhi = $j + 1;	//	mysql中位置
		
		$yilou = [0=>''];
		if($lens[$val['cid']] == 9)	//	9:0-9,10:1-10,11:1-11
		{
			if($rednum[$j] == 0)	//	中奖号中有0
			{
				$yilou[0] = 0;
			}
			else
			{
				//	上次中这个号的时间
				$tmp = $db->getrow('SELECT stoptime FROM '.$itable.' WHERE cid = '.$val['cid'].' AND stoptime < '.$val['stoptime'].' AND FIND_IN_SET(0,rednum) = '.$weizhi.' ORDER BY stoptime DESC');
				if(!$tmp)	//	一直都木有中过，默认遗漏100次
				{
					$yilou[0] = 100;
				}
				else
				{
					$tmp = $db->getrow('SELECT COUNT(1) AS p FROM '.$itable.' WHERE cid = '.$val['cid'].' AND stoptime > '.$tmp['stoptime'].' AND stoptime < '.$val['stoptime']);
					$yilou[0] = intval($tmp['p']) + 1;	//	本期
				}
			}
		}
		
		for($i = 1; $i < 10; $i++)
		{
			if(intval($rednum[$j]) == $i)	//	中奖号中有$i，0$i
			{
				$yilou[] = 0;
			}
			else
			{
				//	上次中这个号的时间
				$tmp = $db->getrow('SELECT stoptime FROM '.$itable.' WHERE cid = '.$val['cid'].' AND stoptime < '.$val['stoptime'].' AND (FIND_IN_SET('.$i.',rednum) = '.$weizhi.' OR FIND_IN_SET(\'0'.$i.'\',rednum) = '.$weizhi.') ORDER BY stoptime DESC');
				if(!$tmp)	//	一直都木有中过，默认遗漏100次
				{
					$yilou[] = 100;
				}
				else
				{
					$tmp = $db->getrow('SELECT COUNT(1) AS p FROM '.$itable.' WHERE cid = '.$val['cid'].' AND stoptime > '.$tmp['stoptime'].' AND stoptime < '.$val['stoptime']);
					$yilou[] = intval($tmp['p']) + 1;	//	本期
				}
			}
		}
		
		if($lens[$val['cid']] > 9)	//	9:0-9,10:1-10,11:1-11
		{
			if($rednum[$j] == 10)	//	中奖号中有10
			{
				$yilou[] = 0;
			}
			else
			{
				//	上次中这个号的时间
				$tmp = $db->getrow('SELECT stoptime FROM '.$itable.' WHERE cid = '.$val['cid'].' AND stoptime < '.$val['stoptime'].' AND FIND_IN_SET(10,rednum) = '.$weizhi.' ORDER BY stoptime DESC');
				if(!$tmp)	//	一直都木有中过，默认遗漏100次
				{
					$yilou[] = 100;
				}
				else
				{
					$tmp = $db->getrow('SELECT COUNT(1) AS p FROM '.$itable.' WHERE cid = '.$val['cid'].' AND stoptime > '.$tmp['stoptime'].' AND stoptime < '.$val['stoptime']);
					$yilou[] = intval($tmp['p']) + 1;	//	本期
				}
			}
			
			if($lens[$val['cid']] == 11)	//	9:0-9,10:1-10,11:1-11
			{
				if($rednum[$j] == 11)	//	中奖号中有11
				{
					$yilou[] = 0;
				}
				else
				{
					//	上次中这个号的时间
					$tmp = $db->getrow('SELECT stoptime FROM '.$itable.' WHERE cid = '.$val['cid'].' AND stoptime < '.$val['stoptime'].' AND FIND_IN_SET(11,rednum) = '.$weizhi.' ORDER BY stoptime DESC');
					if(!$tmp)	//	一直都木有中过，默认遗漏100次
					{
						$yilou[] = 100;
					}
					else
					{
						$tmp = $db->getrow('SELECT COUNT(1) AS p FROM '.$itable.' WHERE cid = '.$val['cid'].' AND stoptime > '.$tmp['stoptime'].' AND stoptime < '.$val['stoptime']);
						$yilou[] = intval($tmp['p']) + 1;	//	本期
					}
				}
			}
		}
		
		$shuju[] = $yilou;
	}
	
	//	上面程序执行时间过长，所以再算一遍，以防重复
	$tmp = $db->getrow('SELECT 1 FROM '.$table.' WHERE iid = '.$val['id']);
	if($tmp)
	{
		continue;
	}
	
	$ins = [];
	$ins['cid']		=	$val['cid'];
	$ins['iid']		=	$val['id'];
	$ins['period']	=	$val['period'];
	$ins['data_0']	=	implode(',',$shuju[0]);
	$ins['data_1']	=	implode(',',$shuju[1]);
	$ins['data_2']	=	implode(',',$shuju[2]);
	$ins['data_3']	=	$shuju[3] ? implode(',',$shuju[3]) : '';
	$ins['data_4']	=	$shuju[4] ? implode(',',$shuju[4]) : '';
	$db->exec('INSERT INTO '.$table.CreateInsertSql($ins));
}

exit('success');